package com.mayfool.task.controller;

import com.mayfool.task.dao.RepositoryDao;
import com.mayfool.task.entity.Repository;
import com.mayfool.task.service.RepositoryService;
import com.mayfool.task.vo.repository.ScanRepositoryReqVO;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zhou.xu 2024/8/27 13:06
 */
@RestController("repository")
@RequiredArgsConstructor
public class RepositoryController {

    private final RepositoryDao repositoryDao;
    private final RepositoryService repositoryService;

    @PostMapping("list")
    public List<Repository> list() {
        return repositoryService.list();
    }

    @GetMapping("{id}")
    public Repository get(@PathVariable Long id) {
        return repositoryService.get(id);
    }

    @PutMapping("{id}")
    @ResponseBody
    public Repository save(@PathVariable(required = false) Long id, @RequestBody Repository repository) {
        repository.setId(id);
        return repositoryService.save(repository);
    }

    @DeleteMapping("{id}")
    public void delete(@PathVariable Long id) {
        repositoryService.delete(id);
    }

    @PostMapping("scan")
    public void scan(@Valid @RequestBody ScanRepositoryReqVO reqVO) {
        repositoryService.scan(reqVO);
    }
}
